package com.sun.showcase.controller.testcase;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.sun.showcase.biz.testcase.excel.CaseLibraryTemplate;
import com.sun.showcase.biz.testcase.service.CaseLibraryService;
import com.sun.showcase.biz.testcase.service.CaseProjectService;
import com.sun.showcase.client.testcase.domain.CaseLibrary;
import com.sun.showcase.client.testcase.domain.CaseProject;
import com.sun.showcase.client.testcase.query.CaseLibraryQuery;
import com.sun.showcase.excel.ExcelExportTemplate;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Json;
import com.sun.showcase.utils.ExecuteResult;
import com.sun.showcase.utils.ServletsUtil;

@Controller
@RequestMapping(value="/testcase/caseLibrary") 
public class CaseLibraryController{
	
	@Autowired
	private CaseLibraryService caseLibraryService;
	@Autowired
	private CaseProjectService caseProjectService;
	
	private Json json = new Json();
	/**
	 * 跳转到CaseLibrary管理页面
	 * 
	 * @return
	 */
	@RequestMapping(value="goCaseLibrary")
	public String goCaseLibrary() {
		return "testcase/caseLibrary";
	}
	/**
	 * 跳转到CaseLibrary管理页面
	 * 
	 * @return
	 */
	@RequestMapping(value="goFrontendCaseLibrary")
	public String goFrontendCaseLibrary(CaseLibraryQuery caseLibraryQuery ,ModelMap map) {
		map.put("libraryCode", caseLibraryQuery.getLibraryCode());
		CaseProject cp = caseProjectService.get(Long.valueOf(caseLibraryQuery.getLibraryCode()));
		map.put("libraryName", cp.getName());
		return "frontend/testcase/caseLibrary";
	}
	/**
	 * 跳转到CaseLibrary新增页面
	 * 
	 * @return
	 */
	@RequestMapping(value="handleCaseLibrary")
	public String handleCaseLibrary(@RequestParam(value="id",required=false) Long id ,ModelMap map) {
		return "testcase/caseLibrary_rowEdit";
	}

	/**
	 * 跳转到查看desc页面
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="showDesc")
	public CaseLibrary showDesc(CaseLibraryQuery caseLibraryQuery) {
		return caseLibraryService.get(caseLibraryQuery);
	}

	/**
	 * 获得分页数据表格
	 */
	@ResponseBody
	@RequestMapping(value = "/datagrid")
	public DataGrid datagrid(CaseLibraryQuery caseLibraryQuery) {
		return caseLibraryService.datagrid(caseLibraryQuery);
	}
	

	/**
	 * 添加一个CaseLibrary
	 */
	@ResponseBody
	@RequestMapping(value="add")
	public Json add(CaseLibraryQuery caseLibraryQuery) {
		caseLibraryService.add(caseLibraryQuery);
		json.setSuccess(true);
		json.setMsg("添加成功！");
		return json;
	}

	/**
	 * 编辑CaseLibrary
	 */
	@ResponseBody
	@RequestMapping(value="edit")
	public Json edit(CaseLibraryQuery caseLibraryQuery) {
		caseLibraryService.update(caseLibraryQuery);
		json.setSuccess(true);
		json.setMsg("编辑成功！");
		return json;
	}

	/**
	 * 物理删除CaseLibrary
	 */
	@ResponseBody
	@RequestMapping(value="delete")
	public Json delete(CaseLibraryQuery caseLibraryQuery) {
		caseLibraryService.delete(caseLibraryQuery.getIds());
		json.setSuccess(true);
		json.setMsg("删除成功！");
		return json;
	}
	
	/**
	 * 逻辑删除CaseLibrary
	 */
	@ResponseBody
	@RequestMapping(value="deletePt")
	public Json deletePt(CaseLibraryQuery caseLibraryQuery) {
		caseLibraryService.deletePt(caseLibraryQuery.getIds());
		json.setSuccess(true);
		json.setMsg("删除成功！");
		return json;
	}
	@RequestMapping(value="export")
	public void export(CaseLibraryQuery caseLibraryQuery,HttpServletResponse response) throws IOException, Exception{
		List<CaseLibraryQuery> userList = caseLibraryService.listAll(caseLibraryQuery);
		ExcelExportTemplate<List<CaseLibraryQuery>> exportListTemplate = new CaseLibraryTemplate();
		ServletsUtil.setHeader(response, "用例库");
		exportListTemplate.doExport(response.getOutputStream(), userList);
	}
	@RequestMapping(value="importExcel")
	@ResponseBody
	public Json importExcel(@RequestParam("file") MultipartFile file) throws IOException, Exception{
		ExecuteResult<CaseLibraryQuery> res = caseLibraryService.importExcelData(file.getInputStream(), file.getOriginalFilename());
		if(res.isSuccess()){
			json.setSuccess(true);
			json.setMsg("成功！");
		}else{
			json.setSuccess(false);
			json.setMsg(res.getErrorMessages().get(0));
		}
		return json;
	}
}
